<template>
	<view class="uni-padding-wrap uni-common-mt">
		<view style="font-size: 30upx;text-align: center;margin: 20upx;">
			<button type="primary" :loading="loading" size="mini" @tap="init()">搜索</button>
			<!-- 没有数据？请打开蓝牙,再
			<text @tap="init()" style="color: #007AFF;padding:0 10upx; font-size: 50upx;">刷新</text>
			试试 -->
		</view>
		<view class="uni-btn-v">
			<view class="itemList" style="display: flex;flex-direction: column;">
				<navigator
					open-type="redirect"
					v-for="val in Jsonlist"
					:key="val.dogId"
					:url="'reportDetails?id=' + val.dogId"
					class="item"
					style="border: 1px solid #EEEEEE;box-shadow: 0upx 5upx 5upx #D9D9D9;
					margin:10upx 20upx;border-radius: 10upx;line-height: 80upx;display: flex;justify-content: space-around;padding: 20upx 0upx;"
				>
					<image :src="val.dogAvatar" mode="" style="width: 300upx;height: 300upx;"></image>
					<view class="" style="display: flex;flex-direction: column;justify-content: space-between;">
						<text style="font-size: 26upx">
						犬只名称:
						<text style="color: #999999;">{{ val.dogName }}</text>
					</text>
					<text style="font-size: 26upx">
						免疫状态:
						<text style="color: #999999;">{{ val.immunity }}</text>
					</text>
					<text style="font-size: 26upx">
						年检状态:
						<text style="color: #999999;">{{ val.checked }}</text>
					</text>
					</view>
					
				</navigator>
			</view>
		</view>
	</view>
</template>
<script>
import { mapState, mapMutations } from 'vuex';
export default {
	computed: mapState(['URL']),
	data() {
		// 初始值
		return {
			Jsonlist: [],
			loading: Boolean,
			dogTags: ''
		};
	},
	onLoad() {
		// 页面加载
		this.init();
	},
	methods: {
		init() {
			this.openBluetoothAdapter(); // 初始化蓝牙
		},
		getDogList() {
			let data = {
				code: '',
				dogTags: this.dogTags,
				pageNumber: '0',
				pageSize: '10'
			};

			uni.request({
				url: this.URL + 'getList',
				data: data,
				success: res => {
					console.log(this.Jsonlist);
					this.Jsonlist = res.data.data;
				}
			});
		},
		/**
		 * 初始化蓝牙设备
		 */
		openBluetoothAdapter() {
			uni.openBluetoothAdapter({
				success: e => {
					this.startBluetoothDevicesDiscovery();
				},
				fail(e) {
					initTypes(e.errCode, e.errMsg);
				}
			});
		},
		/**
		 * 开始搜索蓝牙设备
		 */ startBluetoothDevicesDiscovery() {
			uni.startBluetoothDevicesDiscovery({
				// services: ['1101','FDA50693-A4E2-4FB1-AFCF-C6EB07647825'],//uuid
				success: res => {
					this.onBluetoothDeviceFound();
					this.loading = true;
				},
				fail: e => {
					this.loading = false;
					console.log('搜索蓝牙设备失败，错误码：' + e.errCode);
					if (e.errCode !== 0) {
						initTypes(e.errCode);
					}
				}
			});
		},
		/**
		 * 发现外围设备
		 */
		onBluetoothDeviceFound() {
			uni.onBluetoothDeviceFound(devices => {
				// console.log('开始监听寻找到新设备的事件');
				this.getBluetoothDevices();
			});
		},
		/**
		 * 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
		 */
		getBluetoothDevices() {
			uni.getBluetoothDevices({
				success: res => {
					let changesData = [];
					// console.log(JSON.stringify(res.devices));
					// console.log('获取蓝牙设备成功:' + res.errMsg);
					for (let val of res.devices) {
						if (val.name.slice(0, 4) === 'Mini') {
							changesData.push(val);
						}
					}
					let dd = '';
					changesData.map((val, i) => {
						if (i === 0) {
							dd += val.name;
						} else {
							dd += ',' + val.name;
						}
					});
					console.log(dd);
					this.dogTags = dd;
					if (changesData.length > 0) {
						this.getDogList();
					}
					setTimeout(() => {
						uni.stopBluetoothDevicesDiscovery({
							success: () => {}
						});
						uni.closeBluetoothAdapter({
							success: () => {}
						});
						this.loading = false;
					}, 1000 * 30);
				},
				fail: e => {
					this.loading = false;
					console.log('获取蓝牙设备错误，错误码：' + e.errCode);
					if (e.errCode !== 0) {
						initTypes(e.errCode);
					}
				}
			});
		}
	}
};
/**
 * 判断初始化蓝牙状态
 */
function initTypes(code, errMsg) {
	switch (code) {
		case 10000:
			toast('未初始化蓝牙适配器');
			break;
		case 10001:
			toast('未检测到蓝牙，请打开蓝牙重试！');
			break;
		case 10002:
			toast('没有找到指定设备');
			break;
		case 10003:
			toast('连接失败');
			break;
		case 10004:
			toast('没有找到指定服务');
			break;
		case 10005:
			toast('没有找到指定特征值');
			break;
		case 10006:
			toast('当前连接已断开');
			break;
		case 10007:
			toast('当前特征值不支持此操作');
			break;
		case 10008:
			toast('其余所有系统上报的异常');
			break;
		case 10009:
			toast('Android 系统特有，系统版本低于 4.3 不支持 BLE');
			break;
		default:
			toast(errMsg);
	}
}
/**
 * 弹出框封装
 */
function toast(content, showCancel = false) {
	uni.showModal({
		title: '提示',
		content,
		showCancel
	});
}
</script>

<style>
.uni-title {
	/* width: 100%; */
	/* height: 80upx; */
	text-align: center;
}

.uni-mask {
	position: fixed;
	top: 0;
	left: 0;
	bottom: 0;
	display: flex;
	align-items: center;
	width: 100%;
	background: rgba(0, 0, 0, 0.6);
	padding: 0 30upx;
	box-sizing: border-box;
}

.uni-scroll_box {
	height: 70%;
	background: #fff;
	border-radius: 20upx;
}
.uni-list-box {
	margin: 0 20upx;
	padding: 15upx 0;
	border-bottom: 1px #f5f5f5 solid;
	box-sizing: border-box;
}
.uni-list:last-child {
	border: none;
}
.uni-list_name {
	font-size: 30upx;
	color: #333;
}
.uni-list_item {
	font-size: 24upx;
	color: #555;
	line-height: 1.5;
}

.uni-success_box {
	position: absolute;
	left: 0;
	bottom: 0;
	min-height: 100upx;
	width: 100%;
	background: #fff;
	box-sizing: border-box;
	border-top: 1px #eee solid;
}
.uni-success_sub {
	/* width: 100%%; */
	height: 100upx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 0 30upx;
}
.uni-close_button {
	padding: 0 20upx;
	height: 60upx;
	line-height: 60upx;
	background: #ce3c39;
	color: #ffffff;
	border-radius: 10upx;
}
.uni-success_content {
	height: 600upx;
	margin: 30upx;
	margin-top: 0;
	border: 1px #eee solid;
	padding: 30upx;
}
.uni-content_list {
	padding-bottom: 10upx;
	border-bottom: 1px #f5f5f5 solid;
}
.uni-tips {
	text-align: center;
	font-size: 24upx;
	color: #666;
}
</style>
